#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "SettingItemDelegate.h"
#include <QListWidgetItem>
#include <QHBoxLayout>
#include <QLabel>
#include <QChart>
#include <QChartView>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    init();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::CreateSinCharts(){
    m_chartview1 = new QChartView(this);
    QChart* chart = new QChart();
    chart->setTitle("sin/cos图表测试");

    m_chartview1->setChart(chart);
    //this->setCentralWidget(m_chartview);

    QLineSeries * series1 = new QLineSeries();
    QLineSeries * series2 = new QLineSeries();
    series1->setName("sin曲线");
    series2->setName("cos曲线");
    chart->addSeries(series1);
    chart->addSeries(series2);

    //通过序列添加数值
    qreal t = 0,y1,y2,intv = 0.1;
    int icount = 100;
    for(int i = 0;i < icount;i++){
        y1 = qSin(t);
        series1->append(y1,t);

        y2 = qSin(t +20);
        series2->append(y2,t);

        t = t + intv;
    }

    //创建坐标轴
    QValueAxis *axisX = new QValueAxis;
    axisX->setRange(0,10);
    axisX->setTitleText("time(secs)");
    //axisX->setLabelsAngle(-90);

    QValueAxis *axisY = new QValueAxis;
    axisY->setRange(-2,2);
    axisY->setTitleText("values");
    //axisY->setLabelsAngle(0);

    //chart->addAxis(axisX,Qt::AlignLeft);
    //chart->addAxis(axisY,Qt::AlignBottom);



    chart->setAxisX(axisY,series1);
    chart->setAxisY(axisX,series1);

    chart->setAxisX(axisY,series2);
    chart->setAxisY(axisX,series2);
}

QChartView* MainWindow::CreateLineCharts1(){
    QChartView* chartview = new QChartView(this);
    QChart* chart = new QChart();
    //chart->setTitle("x^2曲线图");

    chartview->setChart(chart);
    QLineSeries* series1 = new QLineSeries();
    series1->append(1, 1);
    series1->append(2, 4);
    series1->append(3, 9);
    series1->append(4, 16);
    series1->append(5, 25);
    series1->append(6, 36);
    chart->addSeries(series1);
    chart->createDefaultAxes();
    chart->legend()->hide();
    chart->axisX()->setLabelsVisible(false);
    chart->axisY()->setLabelsVisible(false);
    chart->setAnimationOptions(QChart::SeriesAnimations);
    QMargins margins(10,10,10,10);
    chart->setMargins(margins);
    return chartview;
}

QChartView* MainWindow::CreateLineCharts2(){
    QChartView* chartview = new QChartView(this);
    QChart* chart = new QChart();
    //chart->setTitle("折线图测试");

    chartview->setChart(chart);
    QLineSeries* series1 = new QLineSeries();
    series1->append(1, 1);
    series1->append(1,2);
    series1->append(9,3);
    series1->append(16,4);
    series1->append(6,5);
    series1->append(1,6);
    series1->append(1,7);
    chart->addSeries(series1);
    chart->legend()->hide();

    //创建坐标轴
    QValueAxis *axisX = new QValueAxis;
    axisX->setLabelsVisible(false);
    //axisX->setRange(0,8);
    //axisX->setTitleText("x");
    //axisX->setLabelsAngle(-90);

    QValueAxis *axisY = new QValueAxis;
    axisY->setLabelsVisible(false);
    //axisY->setRange(0,17);
    //axisY->setTitleText("y");

    //chart->createDefaultAxes();

    chart->setAxisX(axisY,series1);
    chart->setAxisY(axisX,series1);

    chart->setAnimationOptions(QChart::SeriesAnimations);
    QMargins margins(10,10,10,10);
    chart->setMargins(margins);
    return chartview;
}

void MainWindow::init(){
    this->setFixedSize(1000,750);
    this->m_SettingDelegate = new SettingItemDelegate(this);
    ui->listsetting->setItemDelegate(m_SettingDelegate);
    ui->listsetting->setMouseTracking(true);

    ui->listsetting->setStyleSheet(
                "QListWidget {"
                "  background-color: white;"
                "  border: none;"
                "  outline: none;" // 移除选中时的虚线框
                "}"
                "QListWidget::item {"
                "  border-bottom: 1px solid #e0e0e0;" // 项之间的分隔线
                "}"
                "QScrollBar:vertical {"
                "  width: 8px;"
                "  background: transparent;"
                "}"
                );
    ui->title_label->setStyleSheet("background: transparent; color: #4273B0;font-family: 'Microsoft YaHei'; font-size: 25px;font-weight:bold");
    ui->title_label->setText("电磁检测数据采集分析系统");

    ui->title_label_2->setStyleSheet("background: transparent; color: #4273B0;font-family: 'Microsoft YaHei'; font-size: 14px;");
    ui->title_label_2->setText("数据采集分析 > 分析");

    ui->top_widget->setStyleSheet("QWidget { background-color: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #BECAEB, stop:1 #F2F4FB); border-style: none; }");

    {
        QListWidgetItem* item = new QListWidgetItem();
        item->setData(Qt::UserRole,"1");
        QWidget* widget = new QWidget();
        widget->setProperty("delegateManaged",true);

        //设置水平布局
        QHBoxLayout* layout = new QHBoxLayout(widget);
        layout->setContentsMargins(20,5,15,5);
        layout->setSpacing(15);

        //图表展示
        QLabel* label1 = new QLabel(widget);
        label1->setFixedSize(43,43);
        QPixmap p1;
        p1.load(":/image/butterfly.png");
        p1 = p1.scaled(43,43);
        label1->setPixmap(p1);

        layout->addWidget(label1);

        QLabel* label2 = new QLabel("设置",widget);
        label2->setStyleSheet("font-family: 'Microsoft YaHei'; font-size: 18px; color: #333;");
        label2->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
        layout->addWidget(label2,1);

        ui->listsetting->addItem(item);
        ui->listsetting->setItemWidget(item, widget);
    }

    {
        QListWidgetItem* item = new QListWidgetItem();
        item->setData(Qt::UserRole,"2");
        QWidget* widget = new QWidget();
        widget->setProperty("delegateManaged",true);

        //设置水平布局
        QHBoxLayout* layout = new QHBoxLayout(widget);
        layout->setContentsMargins(20,5,15,5);
        layout->setSpacing(15);

        //图表展示
        QLabel* label1 = new QLabel(widget);
        label1->setFixedSize(43,43);
        QPixmap p1;
        p1.load(":/image/down.png");
        p1 = p1.scaled(43,43);
        label1->setPixmap(p1);

        layout->addWidget(label1);

        QLabel* label2 = new QLabel("展示",widget);
        label2->setStyleSheet("font-family: 'Microsoft YaHei'; font-size: 18px; color: #333;");
        label2->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
        layout->addWidget(label2,1);

        ui->listsetting->addItem(item);
        ui->listsetting->setItemWidget(item, widget);
    }

    {
        QListWidgetItem* item = new QListWidgetItem();
        item->setData(Qt::UserRole,"2");
        QWidget* widget = new QWidget();
        widget->setProperty("delegateManaged",true);

        //设置水平布局
        QHBoxLayout* layout = new QHBoxLayout(widget);
        layout->setContentsMargins(20,5,15,5);
        layout->setSpacing(15);

        //图表展示
        QLabel* label1 = new QLabel(widget);
        label1->setFixedSize(43,43);
        QPixmap p1;
        p1.load(":/image/up.png");
        p1 = p1.scaled(43,43);
        label1->setPixmap(p1);

        layout->addWidget(label1);

        QLabel* label2 = new QLabel("提取",widget);
        label2->setStyleSheet("font-family: 'Microsoft YaHei';font-weight:bold; font-size: 25px; color: #333;");
        label2->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
        layout->addWidget(label2,1);

        ui->listsetting->addItem(item);
        ui->listsetting->setItemWidget(item, widget);
    }
    ui->cbx_1->addItem("实型");
    ui->cbx_1->addItem("虚型");
    ui->cbx_1->setStyleSheet("background-color:white;");

    ui->cbx_2->addItems(QStringList() << "1" << "2" << "3" << "4" << "5");
    ui->cbx_2->setStyleSheet("background-color:white;");

    ui->cbx_3->addItems(QStringList() << "1" << "2" << "3" << "4" << "5");
    ui->cbx_3->setStyleSheet("background-color:white;");

    CreateSinCharts();
    m_chartview2 = CreateLineCharts1();
    m_chartview3 = CreateLineCharts2();
    ui->verticalLayout_2->addWidget(m_chartview1,1);
    ui->verticalLayout_3->addWidget(m_chartview2);
    ui->verticalLayout_3->addWidget(m_chartview3);

    QLabel* label_pic1 = new QLabel(this);
    //label_pic1->setFixed
    label_pic1->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Preferred);
    ui->verticalLayout_4->addWidget(label_pic1,0);
    int w = label_pic1->geometry().width();
    int h = label_pic1->geometry().height();
    QPixmap pixmap;
    pixmap.load(":/image/tx1.jpg");
    pixmap = pixmap.scaled(w,2*w);
    label_pic1->setPixmap(pixmap);

    m_chartview4 = CreateLineCharts1();
    m_chartview5 = CreateLineCharts2();

    ui->verticalLayout_4->addWidget(m_chartview4,1);
    ui->verticalLayout_4->addWidget(m_chartview5,1);

    QLabel* label_pic2 = new QLabel(this);
    label_pic1->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Preferred);
    ui->verticalLayout_5->addWidget(label_pic2,1);
    //QPixmap pixmap;
    //pixmap.load(":/image/tx1.jpg");
    //pixmap = pixmap.scaled(w,h);
    label_pic2->setPixmap(pixmap);

    m_chartview6 = CreateLineCharts1();
    m_chartview7 = CreateLineCharts2();

    ui->verticalLayout_5->addWidget(m_chartview6,1);
    ui->verticalLayout_5->addWidget(m_chartview7,1);
    QString buttonStyle1 = "QPushButton { background-color: blue; color: white; }";
    ui->pushButton_1->setStyleSheet(buttonStyle1);
    ui->pushButton_2->setStyleSheet(buttonStyle1);
    ui->pushButton_3->setStyleSheet(buttonStyle1);
    this->setWindowTitle("ECA");
}
